package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.IDictionaryService;
import cn.sxt.clinic.service.IMedicineInfoService;
import cn.sxt.clinic.service.IProcurementTmpService;
import cn.sxt.clinic.service.IWarehousingAuditService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/warehousingAudit")
public class WarehousingAuditController {

    @Autowired
    private IMedicineInfoService medicineInfoService;

    @Autowired
    private IWarehousingAuditService warehousingAuditService;
    
    @Autowired
    private IProcurementTmpService procurementTmpService;

    @RequiresPermissions("warehousingAudit:warehousingAuditManager")
    @RequestMapping("/warehousingAuditManager")
    public String warehousingAuditManager(){
        return "warehousingAuditManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   String keyword){

        IPage<WarehousingAudit> page=new Page<>(pageNum,pageSize);
        QueryWrapper<WarehousingAudit> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("audit_procurement",keyword);
            wrapper.or();
            wrapper.like("audit_reviewer",keyword);
            wrapper.or();
            wrapper.like("supplier",keyword);
        }
        IPage<WarehousingAudit> result = warehousingAuditService.page(page,wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows",result.getRecords());
        return map;
    }

    @RequiresPermissions("warehousingAudit:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultCode delete(Long auditId){

        boolean b = warehousingAuditService.removeById(auditId);
        ResultCode resultCode=null;
        if (b){
            resultCode=new ResultCode(1,"删除成功");
        }else{
            resultCode=new ResultCode(0,"删除失败");
        }
        return resultCode;
    }

    @RequiresPermissions("warehousingAudit:changeStatus")
    @RequestMapping("/changeStatus")
    @ResponseBody
    public ResultCode changeStatus(Long auditId, HttpSession session){
        WarehousingAudit warehousingAudit = warehousingAuditService.getById(auditId);
        warehousingAudit.setStatus(1l);
        Doctor doctor = (Doctor) session.getAttribute("doctor");
        warehousingAudit.setAuditReviewer(doctor.getRealname());
        Long auditMakeNumber = warehousingAudit.getAuditMakeNumber();

        warehousingAudit.insertOrUpdate();

        //添加数据到库存表
        QueryWrapper<ProcurementTmp> wrapper = new QueryWrapper<>();
        wrapper.eq("make_number",auditMakeNumber);
        List<ProcurementTmp> list = procurementTmpService.list(wrapper);
        for (ProcurementTmp procurementTmp : list) {
            Stock stock = new Stock();
            stock.setMedicineName(procurementTmp.getMedicineName());
            stock.setDictionarySpecs(procurementTmp.getSpecs());
            QueryWrapper<MedicineInfo> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("medicine_name",procurementTmp.getMedicineName());
            MedicineInfo medicineInfo = medicineInfoService.getOne(queryWrapper);
            stock.setDictionaryType(medicineInfo.getDictionaryType());
            stock.setStockNumber(procurementTmp.getProcurementNumber().intValue());
            stock.setWholesalePrice(procurementTmp.getProcurementWholesalePrice());
            stock.setPrescriptionPrice(procurementTmp.getProcurementPrescriptionPrice());
            stock.setWholesaleAmount(procurementTmp.getProcurementWholesaleAmount());
            stock.setPrescriptionAmount(procurementTmp.getProcurementPrescriptionAmount());
            stock.insertOrUpdate();
        }
        return new ResultCode(1,"审核成功");
    }

}

